import { Body, Controller, Get, Post, Res, UseInterceptors, Request, Query } from '@nestjs/common';
import utils from 'src/common/utils';
import { ValidRSAInterceptor } from 'src/interceptor/validRSA';
import { ValidTokenInterceptor } from 'src/interceptor/validToken';
import { apibefore } from 'src/router';
import { Notices } from './notices.entity';
import { NoticeCheckEnum, NoticeIsDeleteEnum, NoticeTypeEnum } from './notices.enum';
import { NoticesService } from './notices.service';

@UseInterceptors(ValidRSAInterceptor)
@UseInterceptors(ValidTokenInterceptor)
@Controller(apibefore('/notices'))
export class NoticesController {
  constructor(private readonly service: NoticesService) {}

  @Post('/readNotices')
  async readNotices(@Body() data){
    return await this.service.readNotices(data.ids)
  }

  @Get('/getNotRead')
  async getNotRead(@Request() req){
    return await this.service.getNotRead(req.headers.wx_user)
  }

  @Get('/findAll')
  async findAll(@Query() params, @Request() req){
    let {
      take, skip
    } = utils.getLimit(params.pageSize, params.pageIndex)
    let {dataList, overseas, total} = await this.service.findAll_By_WxUser(req.headers.wx_user,take, skip)
    let returnResult = []
    dataList.map((e:any) => {
      let item = {
        id:e.id,
        video_id:e.video_id,
        create_time:new Date(e.create_time).getTime(),
        notice:'',
        is_read: e.is_read,
        videoType:0,
        is_delete: e.is_delete === NoticeIsDeleteEnum.yes,
        readonly: false,
        video_unable: false
      }
      e.reason = e.reason || e.reject_reason
      switch(e.type){
        case NoticeTypeEnum.answer_accept:
          item.notice = '您的答案已通过审核'
          item.videoType = 4
          if(Number(e.answer || 0) === 0){
            item.video_unable = true
          }
          break
        case NoticeTypeEnum.answer_reject:
          // item.notice = '出处不正确，回答已驳回'
          item.videoType = 4
          if(Number(e.answer || 0) === 0){
            item.video_unable = true
          }
          break
        // case NoticeTypeEnum.answer_exists:
        //   item.notice = '该出处已找到，回答已驳回'
        //   item.videoType = 4
        //   break;
        case NoticeTypeEnum.collection_is_answer:
          item.notice = '您收藏的内容已有答案'
          item.videoType = 2
          if(Number(e.collection || 0) === 0){
            item.video_unable = true
          }
          break
        case NoticeTypeEnum.question_accept:
          item.notice = '您的提问已通过审核'
          item.videoType = 1
          if(Number(e.question_a || 0) === 0 && Number(e.question_c || 0) === 0){
            item.video_unable = true
          }
          break
        case NoticeTypeEnum.question_reject:
          item.notice = '您的提问已被驳回'
          item.videoType = 1
          if(Number(e.question_a || 0) === 0 && Number(e.question_c || 0) === 0){
            item.video_unable = true
          }
          break
        case NoticeTypeEnum.question_is_answer:
          item.notice = '您的提问已有答案'
          item.videoType = 1
          if(Number(e.question_a || 0) === 0 && Number(e.question_c || 0) === 0){
            item.video_unable = true
          }
          break
        case NoticeTypeEnum.works_accept:
          item.notice = '您的作品已通过审核'
          item.videoType = 3
          let count = overseas == 1 ? Number(e.original || 0) : Number(e.original_overseas || 0)
          if(count  === 0){
            item.video_unable = true
          }
          break
        case NoticeTypeEnum.works_reject:
          item.notice = '您的作品已被驳回'
          item.videoType = 3
          let count2 = overseas == 1 ? Number(e.original || 0) : Number(e.original_overseas || 0)
          if(count2  === 0){
            item.video_unable = true
          }
          break
        case NoticeTypeEnum.works_delete:
          item.notice = '您的作品已被删除'
          item.videoType = 3
          let count3 = overseas == 1 ? Number(e.original || 0) : Number(e.original_overseas || 0)
          if(count3  === 0){
            item.video_unable = true
          }
          break
        case NoticeTypeEnum.refund_accept:
          item.notice = `您的退款申请已通过`//${e.consume_time ? '（消费时间' + utils.Format(new Date(e.consume_time),'yyyy-MM-dd hh:mm') + '）' : ''}`
          // item.readonly = true
          if(Number(e.workSource || 0) > 0 || Number(e.workSource2 || 0) > 0){
            item.videoType = 2
            if(Number(e.collection || 0) === 0){
              item.video_unable = true
            }
          }else{
            item.videoType = 1
            if(Number(e.question_a || 0) === 0 && Number(e.question_c || 0) === 0){
              item.video_unable = true
            }
          }
          break
        case NoticeTypeEnum.refund_reject:
          item.notice = `您的退款申请未通过`
          // item.readonly = true
          if(Number(e.workSource || 0) > 0 || Number(e.workSource2 || 0) > 0){
            item.videoType = 2
            if(Number(e.collection || 0) === 0){
              item.video_unable = true
            }
          }else{
            item.videoType = 1
            if(Number(e.question_a || 0) === 0 && Number(e.question_c || 0) === 0){
              item.video_unable = true
            }
          }
          break
        case NoticeTypeEnum.refund_answer_is_error_accept:
          item.notice = '您的退款已自动发起'
          item.videoType = 1
          if(Number(e.question_a || 0) === 0 && Number(e.question_c || 0) === 0){
            item.video_unable = true
          }
          break;
        case NoticeTypeEnum.platform_check_accept:
          item.notice = '您的其他平台信息审核已通过'
          item.readonly = true
          break;
        case NoticeTypeEnum.platform_check_reject:
          item.notice = '您的其他平台信息审核被驳回'
          item.readonly = true
          break;
        case NoticeTypeEnum.answer_is_update:
          item.notice = e.content
          if(Number(e.video_question || 0) > 0){
            item.videoType = 1
          }else if(Number(e.video_original || 0) > 0){
            item.videoType = 3
          }else if(Number(e.video_collection || 0) > 0){
            item.videoType = 2
          }
          break;
      }
      if(e.reason){
        item.notice = (item.notice ? item.notice + '，' : '') + e.reason
      }
      // if(e.type === NoticeTypeEnum.refund_reject){
      //   item.notice += `${e.profit_time ? '（消费时间' + utils.Format(new Date(e.profit_time),'yyyy-MM-dd hh:mm') + '）' : ''}`
      // }
      returnResult.push(item)
    })
    return {data:returnResult, total}
  }
}
